package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public UserVO findUserInfoByUsername(String username) {
        //     根据账号查询 用户对应角色信息
          UserVO  userVO= baseMapper.findUserByUsername(username);
          //  根据用户id  查询对应得角色信息
        List<RoleVO> roleVOList =  baseMapper.findRoleInfoByUserId(userVO.getId());
        //  权限信息
        for (RoleVO roleVO : roleVOList) {
            List<Permission> permissionList = baseMapper.findPermissionInfoByRoleId(roleVO.getId());
            roleVO.setPermissionsList(permissionList);
        }
          userVO.setRoleVOList(roleVOList);
        return userVO;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryString)) {
            queryWrapper.like("name", queryString);
        }

        Page<User> page = page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);

        List<User> list = page.getRecords();

        List<UserVO> userVOS = UserListToUserVoList(list);
        return new PageResult(page.getTotal(), userVOS);
    }

    @Override
    @LcnTransaction
    public Boolean saveOrUpdateVo(UserVO user) {
        boolean b = saveOrUpdate(user);
        List<String> roleNameList = user.getRoleNameList();
        baseMapper.deleteUserRoleByUserID(user.getId());
        for (String s : roleNameList) {
            baseMapper.saveUserRoleByRoleName(user.getId(),s);
        }
        return b;
    }

    @Override
    public Boolean removeUserById(Integer id) {
        boolean b = removeById(id);
        baseMapper.deleteUserRoleByUserID(id);
        return b;
    }

    @Override
    public Boolean checkUsername(Integer username) {
        List<User> username1 = baseMapper.selectList(new QueryWrapper<User>().eq("username", username));
        if (username1.size()>0){
            return false;
        }
        return true;
    }

    private List<UserVO> UserListToUserVoList(List<User> list) {
        ArrayList<UserVO> userVos = new ArrayList<>();
        for (User user : list) {
            UserVO userVO = new UserVO();
            userVO.setId(user.getId());
            userVO.setGender(user.getGender());
            userVO.setUsername(user.getUsername());
            userVO.setRemark(user.getRemark());
            userVO.setStation(user.getStation());
            userVO.setTelephone(user.getTelephone());
            userVO.setBirthdayString(new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()));
            userVO.setRoleNameList(baseMapper.findUserAllRoleByUserId(user.getId()));
            userVos.add(userVO);
        }

        return userVos;
    }
}
